package cate.game.server.service;

import cate.game.GameBody;
import cate.game.ManyGame;
import cate.game.role.Role;
import easy.java.practice.errorcode.EcException;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UsefulCtx {

    @Getter
    protected ManyGame games;
    @Autowired
    public void setGames(ManyGame games) {
        this.games = games;
    }

    public String getSecret() {
        return games.config.secret;
    }

    public GameBody getGame(int srvId) {
        GameBody game = games.get(srvId);
        if(game == null) {
            throw new EcException("该进程中找不到srvId="+srvId+"的游戏");
        }
        return game;
    }

    public Role getRole(int srvId, String roleId) {
        GameBody game = getGame(srvId);
        Role role = game.role.getRole(roleId);
        if(role == null) {
            throw new EcException("找不到roleId="+roleId+"的角色");
        }
        return role;
    }

}
